package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.NewDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.NewsMapper;
import com.heima.wemedia.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 嘉德罗斯
 * @date 2021/11/29
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper,WmNews> implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public ResponseResult list(NewDto dto) {
        LambdaQueryWrapper<WmNews> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(WmNews::getPublishTime);
        queryWrapper.eq(dto.getStatus()!=null,WmNews::getStatus,dto.getStatus());
        queryWrapper.like(dto.getTitle()!=null,WmNews::getTitle,dto.getTitle());
        IPage<WmNews> page= new Page<>(dto.getPage(), dto.getSize());
        IPage<WmNews> wmNewsIPage = newsMapper.selectPage(page, queryWrapper);
        List<WmNews> records = wmNewsIPage.getRecords();
        return ResponseResult.okResult(records);
    }

    @Override
    public ResponseResult find(Integer id) {
        WmNews wmNews = newsMapper.selectById(id);
        return ResponseResult.okResult(wmNews);
    }

    @Override
    public ResponseResult authPass(NewDto dto) {
        WmNews wmNews = new WmNews();
        wmNews.setId(dto.getId());
        wmNews.setStatus(dto.getStatus());
        wmNews.setSubmitedTime(new Date());
        wmNews.setReason("审核通过");
        newsMapper.updateById(wmNews);
        return ResponseResult.okResult("审核通过");
    }

    @Override
    public ResponseResult authFail(NewDto dto) {
        WmNews wmNews = new WmNews();
        wmNews.setId(dto.getId());
        wmNews.setStatus(dto.getStatus());
        wmNews.setReason(dto.getMsg());
        newsMapper.updateById(wmNews);
        return ResponseResult.okResult("驳回成功");
    }
}
